/*
 * Copyright 2009 IT Mill Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package no.uib.hplims;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import no.uib.hplims.views.MainWindow;

import org.vaadin.appfoundation.authentication.LogoutEvent;
import org.vaadin.appfoundation.authentication.LogoutListener;
import org.vaadin.appfoundation.authentication.SessionHandler;
import org.vaadin.appfoundation.authorization.Permissions;
import org.vaadin.appfoundation.authorization.jpa.JPAPermissionManager;
import org.vaadin.appfoundation.view.ViewHandler;

import com.vaadin.Application;
import com.vaadin.terminal.gwt.server.HttpServletRequestListener;
import com.vaadin.ui.Window;

/**
 * The Application's "main" class
 */
@SuppressWarnings("serial")
public class MyVaadinApplication extends Application implements
		HttpServletRequestListener, LogoutListener {

	private static ThreadLocal<MyVaadinApplication> threadLocal = new ThreadLocal<MyVaadinApplication>();

	private Controller controller = null;

	private MainWindow mainLayout;

	@Override
	public void init() {
		setInstance(this); // So that we immediately have access to the current
							// application

		SessionHandler.initialize(this);
		ViewHandler.initialize(this);
		Permissions.initialize(this, new JPAPermissionManager());

		// Import initial test data
		InitialData.init();

		if (controller == null) {
			controller = new Controller();
		}

		Window mainWindow = new Window("HP LIMS");
		mainWindow.setTheme("hplims");
		mainWindow.setSizeFull();

		mainLayout = new MainWindow();
		// Use the MainLayout as the content of the window. Maximize its size.
		mainWindow.setContent(mainLayout);
		mainWindow.setSizeFull();

		setMainWindow(mainWindow);
	}

	public void logout(LogoutEvent event) {
		// The user has logged out, remove any current views and show the login
		// screen instead
	}

	public static MyVaadinApplication getInstance() {
		return threadLocal.get();
	}

	public static void setInstance(MyVaadinApplication application) {
		threadLocal.set(application);
	}

	public void onRequestStart(HttpServletRequest request,
			HttpServletResponse response) {
		MyVaadinApplication.setInstance(this);
	}

	public void onRequestEnd(HttpServletRequest request,
			HttpServletResponse response) {
		threadLocal.remove();
	}

	/**
	 * @return the controller
	 */
	public Controller getController() {
		if (controller == null) {
			controller = new Controller();
		}
		return controller;
	}
}
